package com.magicsoft.share.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.WindowManager;

import com.gyf.barlibrary.ImmersionBar;
import com.magicsoft.share.constant.AppDavikActivityMgr;
import com.magicsoft.share.constant.Constant;

import butterknife.ButterKnife;

/**
 * Created by lyf on 2017/8/16.
 */

public abstract class BaseActivity<V extends BaseView, T extends BasePresenter<V>> extends AppCompatActivity {

    public T mPresenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }
        ButterKnife.bind(this);
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        mPresenter = initPresenter();
        if (mPresenter != null) {
            mPresenter.attach((V) this);
        }
        initData();
        initView();

        AppDavikActivityMgr.getScreenManager().addActivity(this);
        Log.d("base", "onCreate: " + AppDavikActivityMgr.getScreenManager().getCurrentActivityName());

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.attach((V) this);
        }
        requestData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detach();
            mPresenter.onDestroy();
        }
        ImmersionBar.with(this).destroy();
        AppDavikActivityMgr.getScreenManager().removeActivity(this);
    }


    public abstract T initPresenter();

    /**
     * 对应的布局.
     */
    @LayoutRes
    public abstract int getLayoutId();

    /**
     * 准备数据
     */
    public abstract void initData();

    /**
     * 初始化view
     */
    public abstract void initView();


    /**
     * 请求数据
     */
    public abstract void requestData();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

            if (requestCode == Constant.REFRESH_UI) {
                requestData();
            }
        }
    }
}
